package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Reference
    private BrandService brandService;

    @RequestMapping("list")
    public String list(){

        return "admin/brand";
    }

    @RequestMapping("listBrands")
    @ResponseBody
    public Result listBrands(){
        List<TbBrand> list = brandService.listBrands();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }


    @RequestMapping("insertBrandInfo")
    @ResponseBody
    public Result insertBrandInfo(TbBrand tbBrand){
        if (tbBrand == null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        brandService.insertBrandInfo(tbBrand);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("updateBrand/{id}")
    @ResponseBody
    public TbBrand updateBrand(@PathVariable("id")Long id){
        TbBrand tbBrand = brandService.updateBrand(id);
        return tbBrand;
    }


    @RequestMapping("updateBrandInfo")
    @ResponseBody
    public Result updateBrandInfo(TbBrand tbBrand){
        if (tbBrand == null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        brandService.updateBrandInfo(tbBrand);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("deleteBrandInfo")
    @ResponseBody
    public Result deleteBrandInfo(@RequestParam("ids") Long[] id){
        if (id == null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
            brandService.deleteBrandInfo(id);
        return Result.build(ConstantEnum.SUCCESS);
    }

}
